import { _decorator, Animation, Node, RichText, Label, Button, BlockInputEvents, AnimationClip } from 'cc';
import Dialog from './Dialog';
import SoundCtrl from '../../Ctrl/SoundCtrl';
const { ccclass, property } = _decorator;

export interface TextDialogData {
    text?: string,
    title?: string,
    leftBtnClick?: (dialog?: TextDialog) => void,
    rightBtnClick?: (dialog?: TextDialog) => void,
    closeBtnClick?: (dialog?: TextDialog) => void,
    leftBtnTitle?: string,
    rightBtnTitle?: string,
    noTouchMask?: boolean;
}

/**
 * 通用文本弹框
 */
@ccclass('TextDialog')
export default class TextDialog extends Dialog {

    @property(AnimationClip)
    protected closeAnim: AnimationClip = null;
    @property(Label)
    protected title: Label = null;

    @property({
        displayName: "描述文本",
        type: Label
    })
    protected text: Label = null;

    protected data: TextDialogData = null;

    @property({ type: Button })
    protected leftBtn: Button = null;
    @property({ type: Button })
    protected rightBtn: Button = null;

    protected leftBtnTitle: Label = null;
    protected rightBtnTitle: Label = null;

    protected animation: Animation = null;

    protected onLeftBtnClick() {
        SoundCtrl.playEffect('mouseclick1');
        this.data.leftBtnClick && this.data.leftBtnClick(this);
    }

    protected onRightBtnClick() {
        SoundCtrl.playEffect('mouseclick1');
        this.data.rightBtnClick && this.data.rightBtnClick(this);
    }

    onLoad() {
        if (this.leftBtn) {
            this.leftBtnTitle = this.leftBtn.getComponentInChildren(Label);
        }
        if (this.rightBtn) {
            this.rightBtnTitle = this.rightBtn.getComponentInChildren(Label);
        }
        if (this.closeAnim) {
            this.animation = this.node.getComponent(Animation) || this.node.addComponent(Animation);
            this.animation.addClip(this.closeAnim);
        }
    }

    setData(data: TextDialogData) {
        this.data = data;
        this.text.string = data.text;
        if (data.title && this.title) {
            this.title.string = data.title
        }
        if (data.leftBtnTitle && this.leftBtnTitle) {
            this.leftBtnTitle.string = data.leftBtnTitle;
        }
        if (data.rightBtnTitle && this.rightBtnTitle) {
            this.rightBtnTitle.string = data.rightBtnTitle;
        }
        if (data.leftBtnClick) {
            this.leftBtn.node.active = true;
            if (!data.rightBtnClick && this.rightBtn) {
                this.rightBtn.node.active = false;
                this.leftBtn.node.setPosition(0, this.leftBtn.node.position.y);
            }
        }
        else if (data.rightBtnClick) {
            this.rightBtn.node.active = true;
            if (!data.leftBtnClick && this.leftBtn) {
                this.leftBtn.node.active = false;
                this.rightBtn.node.setPosition(0, this.rightBtn.node.position.y);
            }
        }
        if (data.noTouchMask) {
            this.getComponent(BlockInputEvents).enabled = false;
        }
        SoundCtrl.playEffect('forceField_001');
        return this;
    }

    onClose() {
        if (this.data.closeBtnClick) {
            this.data.closeBtnClick(this);
        }
        if (this.closeAnim) {
            this.animation.once(Animation.EventType.FINISHED, () => {
                this.close();
            });
            this.animation.play(this.closeAnim.name);
        }
        else {
            this.close();
        }
        SoundCtrl.playEffect('forceField_000');
    }

}


